package bairui.international.console.web.controller.branch.branch.staff;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;

import bairui.international.model.domain.jpa.QTB_User;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.service.Service_User;

/**
 *
 * 添加
 *
 * @generated
 * 
 * @see STest_C_BranchCompanyStaff_Add_Page 测试
 *
 */
@Controller
public class C_BranchCompanyStaff_Add_Page
 extends SC_BranchCompanyStaff_Add_Page 
{
	
	@Autowired private Service_User service_User;
//	private final static QTB_BranchCompanyStaff QBranchCompanyStaff = QTB_BranchCompanyStaff.tB_BranchCompanyStaff;
	
	/**
	 * 分页列表
	 */
	@Action(Page.class)
	@GetMapping(value=Page.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object page(
		HttpServletRequest request, 
		@PathVariable(name=Page.PathVariableName.BranchId) long branchId
	) throws Exception {
		org.springframework.data.domain.Page<TB_User> page = service_User.page(
				it->filter(it, request), 
				Page.Output.page(request), 
				it->sort(it, request)
			);
		//
		return Page.Output.output(
				page
			);
	}
	
	/*
	 * 过滤
	 */
	static BooleanBuilder filter(QTB_User it, HttpServletRequest request){
		return Page.Filter.query(it, request)
				// 排除已经添加为员工的用户
				.and(it.branchCompanyStaffs.isEmpty()
						.or(it.branchCompanyStaffs.any().activeFlag.isFalse())
					);
//				.and(it.notIn(
//					JPAExpressions
//						.select(QBranchCompanyStaff.user)
//						.from(QBranchCompanyStaff)
//						.where(
//							QBranchCompanyStaff.user.eq(it)
//						)
//				));
	}
	
	/*
	 * 排序
	 */
	static Sort sort(QTB_User it, HttpServletRequest request){
		return Page.Output.sort(it, request);
	}
	
	

}
